# -*- coding: utf-8 -*-
import os
import configparser

cnf_file = "myback.ini"
config = configparser.ConfigParser()
config.read(cnf_file)

binlog_sql_dir = config.get("myback", "binlog_sql_dir")
slave_container_name = config.get("slave", "slave_container_name")
slave_user = config.get("slave", "slave_user")
slave_password = config.get("slave", "slave_password")


def run_database_binlog():
    binlog_sql_files = os.listdir(binlog_sql_dir)
    if not binlog_sql_files:
        print("无可用于恢复的binlog sql")
        exit(1)

    print(binlog_sql_files)
    binlog_sql_files.sort()

    for binlog_sql in binlog_sql_files:
        pass
        restore_commond = '''docker exec -i {0} mysql -u{1} -p{2} -f --max_allowed_packet=1073741824 < {3}/{4}''' \
            .format(slave_container_name, slave_user, slave_password, binlog_sql_dir, binlog_sql)
        print("execute the following command....")
        print(restore_commond)
        os.system(restore_commond)


if __name__ == '__main__':
    run_database_binlog()
